<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态文字弹幕</title>
    <style>
        :root {
            --primary: #4361ee;
            --secondary: #3a0ca3;
            --accent: #f72585;
            --light: #f8f9fa;
            --dark: #212529;
            --gray: #e9ecef;
            --success: #4cc9f0;
            --radius: 12px;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
        }

        body {
            min-height: 100vh;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            color: var(--dark);
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .header {
            text-align: center;
            margin-bottom: 20px;
            width: 100%;
            max-width: 800px;
        }

        .header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            color: var(--primary);
            font-weight: 800;
        }

        .header p {
            font-size: 1.1rem;
            color: #6c757d;
        }

        /* === 视频容器 === */
        #video-container {
            position: relative;
            width: 100%;
            max-width: 800px;
            height: 450px;
            margin: 0 auto 20px;
            background: #fff;
            border-radius: var(--radius);
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        #container {
            position: relative;
            width: 100%;
            height: 100%;
            background: #000;
            border-radius: var(--radius);
        }

        /* === 弹幕 === */
        .bullet {
            position: absolute;
            padding: 8px 16px;
            border-radius: 20px;
            font-size: 16px;
            font-weight: 500;
            white-space: nowrap;
            cursor: pointer;
            transition: all .3s ease;
            box-shadow: 0 4px 10px rgba(0,0,0,0.1);
            color: #fff !important;
            backdrop-filter: blur(4px);
        }

        .bullet:hover {
            transform: scale(1.05);
            z-index: 10;
            box-shadow: 0 6px 15px rgba(0,0,0,0.2);
        }

        .delete-text {
            margin-left: 8px;
            padding: 3px 8px;
            font-size: 12px;
            background: var(--accent);
            color: #fff;
            border-radius: 12px;
            display: none;
        }

        .bullet:hover .delete-text {
            display: inline-block;
        }

        /* === 输入区 === */
        .input-section {
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            width: 90%;
            display: flex;
            background: rgba(255,255,255,0.95);
            border-radius: var(--radius);
            padding: 12px;
            gap: 10px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.1);
            z-index: 5;
        }

        .input-section input {
            flex: 1;
            padding: 12px 20px;
            border: 2px solid var(--gray);
            border-radius: var(--radius);
            outline: none;
            background: #fff;
            color: var(--dark);
            font-size: 16px;
            transition: border-color 0.3s;
        }

        .input-section input:focus {
            border-color: var(--primary);
        }

        .input-section button {
            padding: 12px 25px;
            border: none;
            border-radius: var(--radius);
            background: var(--primary);
            color: #fff;
            font-weight: 600;
            cursor: pointer;
            transition: .3s;
        }

        .input-section button:hover {
            background: var(--secondary);
            transform: translateY(-2px);
        }

        /* === 控制区 === */
        .controls {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            width: 100%;
            max-width: 800px;
            background: #fff;
            border-radius: var(--radius);
            padding: 20px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.05);
            margin-bottom: 20px;
        }

        .control-group {
            flex: 1;
            min-width: 200px;
        }

        .control-group h3 {
            margin-bottom: 12px;
            color: var(--dark);
            font-size: 1.1rem;
        }

        .color-picker {
            display: flex;
            gap: 8px;
            flex-wrap: wrap;
        }

        .color-option {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            cursor: pointer;
            border: 2px solid transparent;
            transition: .2s;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        .color-option.active,
        .color-option:hover {
            transform: scale(1.15);
            border: 2px solid #fff;
            box-shadow: 0 3px 8px rgba(0,0,0,0.2);
        }

        .slider-container {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .slider-container input {
            flex: 1;
            -webkit-appearance: none;
            height: 6px;
            border-radius: 3px;
            background: var(--gray);
            outline: none;
        }

        .slider-container input::-webkit-slider-thumb {
            -webkit-appearance: none;
            width: 18px;
            height: 18px;
            border-radius: 50%;
            background: var(--primary);
            cursor: pointer;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
        }

        /* === 历史区 === */
        .history {
            width: 100%;
            max-width: 800px;
            background: #fff;
            border-radius: var(--radius);
            padding: 20px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.05);
        }

        .history h2 {
            margin-bottom: 15px;
            text-align: center;
            color: var(--dark);
        }

        .history-list {
            max-height: 150px;
            overflow-y: auto;
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
        }

        .history-item {
            background: var(--gray);
            padding: 8px 15px;
            border-radius: 20px;
            cursor: pointer;
            transition: .2s;
            font-size: 14px;
        }

        .history-item:hover {
            background: var(--primary);
            color: #fff;
            transform: translateY(-2px);
        }

        /* 弹幕统计 */
        .stats {
            display: flex;
            justify-content: space-between;
            margin-top: 10px;
            font-size: 14px;
            color: #6c757d;
        }

        /* === 响应式 === */
        @media(max-width:600px) {
            .input-section {
                flex-direction: column;
            }

            .header h1 {
                font-size: 2rem;
            }
            
            .controls {
                flex-direction: column;
                gap: 15px;
            }
        }
    </style>
</head>
<body>
<div class="header">
    <h1>动态文字弹幕</h1>
    
</div>

<div id="video-container">
    <div id="container"></div>
    <div class="input-section">
        <input type="text" id="messageInput" placeholder="输入弹幕内容...">
        <button id="sendButton">发送弹幕</button>
    </div>
</div>

<div class="controls">
    <div class="control-group">
        <h3>弹幕颜色</h3>
        <div class="color-picker" id="colorPicker">
            <div class="color-option active" style="background-color: #4361ee;" data-color="#4361ee"></div>
            <div class="color-option" style="background-color: #f72585;" data-color="#f72585"></div>
            <div class="color-option" style="background-color: #4cc9f0;" data-color="#4cc9f0"></div>
            <div class="color-option" style="background-color: #7209b7;" data-color="#7209b7"></div>
            <div class="color-option" style="background-color: #3a0ca3;" data-color="#3a0ca3"></div>
            <div class="color-option" style="background-color: #4895ef;" data-color="#4895ef"></div>
        </div>
    </div>

    <div class="control-group">
        <h3>弹幕速度</h3>
        <div class="slider-container">
            <input type="range" id="speedSlider" min="1" max="10" value="5">
            <span id="speedValue">5</span>
        </div>
    </div>

    <div class="control-group">
        <h3>减速效果</h3>
        <div class="slider-container">
            <input type="range" id="slowdownSlider" min="1" max="10" value="5">
            <span id="slowdownValue">5</span>
        </div>
    </div>
</div>

<div class="history">
    <h2>发送历史</h2>
    <div class="stats">
        <span id="totalSent">已发送: 0</span>
        <span id="activeBullets">活跃弹幕: 0</span>
    </div>
    <div class="history-list" id="historyList"></div>
</div>

<script>
    // 获取DOM元素
    const container = document.getElementById('container');
    const messageInput = document.getElementById('messageInput');
    const sendButton = document.getElementById('sendButton');
    const colorPicker = document.getElementById('colorPicker');
    const speedSlider = document.getElementById('speedSlider');
    const speedValue = document.getElementById('speedValue');
    const slowdownSlider = document.getElementById('slowdownSlider');
    const slowdownValue = document.getElementById('slowdownValue');
    const historyList = document.getElementById('historyList');
    const totalSent = document.getElementById('totalSent');
    const activeBullets = document.getElementById('activeBullets');

    // 定义弹幕数据
    let data = [];
    let speed = 5;
    let slowdownFactor = 5;
    let interval = 800;
    let topArr = [];
    let index = 0;
    let selectedColor = '#4361ee';
    let history = [];
    let sentCount = 0;
    let activeCount = 0;

    // 初始化弹幕轨道
    for (let i = 0; i < 12; i++) {
        topArr.push(i * 35);
    }

    // 设置颜色选择器
    colorPicker.querySelectorAll('.color-option').forEach(option => {
        option.addEventListener('click', () => {
            colorPicker.querySelector('.active').classList.remove('active');
            option.classList.add('active');
            selectedColor = option.dataset.color;
        });
    });

    // 设置速度滑块
    speedSlider.addEventListener('input', () => {
        speed = parseInt(speedSlider.value);
        speedValue.textContent = speed;
    });

    // 设置减速滑块
    slowdownSlider.addEventListener('input', () => {
        slowdownFactor = parseInt(slowdownSlider.value);
        slowdownValue.textContent = slowdownFactor;
    });

    // 发送弹幕
    sendButton.addEventListener('click', sendDanmu);
    messageInput.addEventListener('keypress', function(event) {
        if (event.key === 'Enter') {
            sendDanmu();
        }
    });

    function sendDanmu() {
        const message = messageInput.value.trim();
        if (message) {
            data.push({ text: message, color: selectedColor });
            sentCount++;
            totalSent.textContent = `已发送: ${sentCount}`;

            // 添加到历史记录
            addToHistory(message);

            messageInput.value = '';
            messageInput.focus();
        }
    }

    // 添加到历史记录
    function addToHistory(message) {
        history.push(message);
        if (history.length > 10) {
            history.shift();
        }
        updateHistoryList();
    }

    // 更新历史记录列表
    function updateHistoryList() {
        historyList.innerHTML = '';
        history.forEach((msg, index) => {
            const item = document.createElement('div');
            item.className = 'history-item';
            item.textContent = msg;
            item.addEventListener('click', () => {
                messageInput.value = msg;
                messageInput.focus();
            });
            historyList.appendChild(item);
        });
    }

    // 更新活跃弹幕计数
    function updateActiveCount() {
        activeBullets.textContent = `活跃弹幕: ${activeCount}`;
    }

    // 定时发送弹幕
    setInterval(function() {
        if (data.length > 0) {
            const bulletData = data[index];

            // 创建弹幕元素
            const bullet = document.createElement('div');
            bullet.className = 'bullet';
            bullet.style.backgroundColor = bulletData.color;
            bullet.textContent = bulletData.text;

            // 随机获取弹幕位置
            const top = topArr[Math.floor(Math.random() * topArr.length)];
            bullet.style.top = top + 'px';

            // 将弹幕元素添加到容器中
            container.appendChild(bullet);
            activeCount++;
            updateActiveCount();

            // 计算弹幕宽度
            const width = bullet.offsetWidth;

            // 定义弹幕起始位置
            let left = container.offsetWidth;
            bullet.style.left = left + 'px';

            // 定义弹幕移动定时器
            const bulletTimer = setInterval(function() {
                // 判断是否鼠标悬浮
                if (bullet.isHovered) {
                    left -= speed / slowdownFactor;
                } else {
                    left -= speed;
                }
                bullet.style.left = left + 'px';

                // 判断弹幕是否移出容器
                if (left < -width) {
                    // 移除弹幕元素
                    container.removeChild(bullet);
                    // 清除弹幕移动定时器
                    clearInterval(bulletTimer);
                    activeCount--;
                    updateActiveCount();
                }
            }, 10);

            // 保存定时器ID以便删除
            bullet.bulletTimer = bulletTimer;

            // 更新弹幕索引
            index = (index + 1) % data.length;

            // 为弹幕添加事件监听
            bullet.addEventListener('mouseenter', function() {
                this.isHovered = true;

                // 创建删除按钮
                if (!this.querySelector('.delete-text')) {
                    const deleteText = document.createElement('span');
                    deleteText.className = 'delete-text';
                    deleteText.textContent = '删除';
                    this.appendChild(deleteText);
                }
            });

            bullet.addEventListener('mouseleave', function() {
                this.isHovered = false;

                // 移除删除按钮
                const deleteText = this.querySelector('.delete-text');
                if (deleteText) {
                    this.removeChild(deleteText);
                }
            });
        }
    }, interval);
    
    // 删除弹幕
    container.addEventListener('click', function(event) {
        // 如果点击的是删除按钮
        if (event.target.classList.contains('delete-text')) {
            const bullet = event.target.parentElement;
            clearInterval(bullet.bulletTimer);
            container.removeChild(bullet);
            activeCount--;
            updateActiveCount();
        }
    });
    
    // 初始化一些示例弹幕
    setTimeout(() => {
        data.push({ text: '欢迎使用简约弹幕系统！', color: '#4361ee' });
        data.push({ text: '试试悬浮减速效果', color: '#f72585' });
        data.push({ text: '鼠标悬停可减速查看', color: '#4cc9f0' });
        data.push({ text: '点击删除按钮可移除弹幕', color: '#7209b7' });
        data.push({ text: '可以调整弹幕速度和减速效果', color: '#3a0ca3' });
    }, 1000);
</script>
</body>
</html>